﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Timers;
using com.lover.astd.common.model;
using com.lover.astd.common.model.enumer;
using com.lover.astd.common.partner;
using com.lover.astd.common.partner.impl;
using com.lover.common;
using com.lover.common.http;

namespace com.lover.astd.common.manager
{
    public class LoginMgr
    {
        private static LoginMgr _instance = new LoginMgr();
        public static LoginMgr getInstance() { return _instance; }

        private ILoginReceiver _client;
        private LoginImplBase _partner;
        public LoginResult doLogin(ILoginReceiver loginClient, ref List<Cookie> cookies, string verify_code = null, string extra = null) 
        {
            if (loginClient == null) return null;
            _client = loginClient;
            AccountData acc = loginClient.getAccount();
            _partner = getLoginImpl(acc.Server_type);
            if (_partner == null) return null;
            _partner.setAccount(acc);
            LoginResult result = null;
            try
            {
                result = _partner.login(ref cookies, verify_code, extra);
            }
            catch (Exception ex)
            {
                UiUtils.getInstance().error("ERROR:::" + ex.StackTrace);
            }
            return result;
        }
        public LoginMgr() { }
        public Image getCaptchaImage(string url, ref List<Cookie> cookies)
        {
            HttpResult resp = TransferMgr.doGetPure(url, ref cookies);
            if (resp == null) return null;
            Stream stream = resp.ContentStream;
            Image img = Image.FromStream(stream);
            stream.Close();
            return img;
        }
        public void sendStatus(string status)
        {
            if (_client != null) _client.setLoginStatus(status);
        }
        private LoginImplBase getLoginImpl(ServerType stype)
        {
            LoginImplBase logpartner = null;
            try
            {
                switch (stype)
                {
                    //TODO:4399 not completed
                    case ServerType._178: logpartner = new _178Login(); break;
                    case ServerType._360: logpartner = new _360Login(); break;
                    case ServerType._37Wan: logpartner = new _37WanLogin(); break;
                    case ServerType._3896: logpartner = new _3896Login(); break;
                    case ServerType._4399: logpartner = new _4399Login(); break;
                    case ServerType._51Wan: logpartner = new _51wanLogin(); break;
                    case ServerType._56uu: logpartner = new _56uuLogin(); break;
                    case ServerType._6711: logpartner = new _6711Login(); break;
                    case ServerType._6998: logpartner = new _6998Login(); break;
                    case ServerType._8ZY: logpartner = new _8zyLogin(); break;
                    case ServerType.Aoshitang: logpartner = new AoshitangLogin(); break;
                    case ServerType.Baidu: logpartner = new BaiduLogin(); break;
                    //case ServerType.Custom: logpartner = new CustomServer(); break;
                    case ServerType.DuoWan: logpartner = new DuowanLogin(); break;
                    case ServerType.HuanLang: logpartner = new HuanlangLogin(); break;
                    case ServerType.IFeng: logpartner = new IfengLogin(); break;
                    case ServerType.ISpeak: logpartner = new IspeakLogin(); break;
                    case ServerType.Jinjuzi: logpartner = new JinjuziLogin(); break;
                    case ServerType.KuWan8: logpartner = new Kuwan8Login(); break;
                    case ServerType.PeiYou: logpartner = new PeiyouLogin(); break;
                    case ServerType.PPS: logpartner = new PpsLogin(); break;
                    case ServerType.TgBus: logpartner = new TgbusLogin(); break;
                    case ServerType.VeryCd: logpartner = new VerycdLogin(); break;
                    case ServerType.WebXGame: logpartner = new WebxgameLogin(); break;
                    case ServerType.XdWan: logpartner = new XdwanLogin(); break;
                    case ServerType.YaoWan: logpartner = new YaoWanLogin(); break;
                    case ServerType.ZhuLang: logpartner = new ZhulangLogin(); break;
                    case ServerType.Uz73: logpartner = new Uz73Login(); break;
                    case ServerType.MiYou: logpartner = new MiyouLogin(); break;
                    case ServerType.RenRen: logpartner = new RenrenLogin(); break;
                    case ServerType.Kunlun: logpartner = new KunlunLogin(); break;
                    case ServerType.NiuA: logpartner = new NiuaLogin(); break;
                    case ServerType.Game2: logpartner = new Game2Login(); break;
                    case ServerType._91: logpartner = new _91Login(); break;
                    case ServerType.Cga: logpartner = new CgaLogin(); break;
                    case ServerType.Plu: logpartner = new PluLogin(); break;
                    case ServerType._53Wan: logpartner = new _53wanLogin(); break;
                    case ServerType._789hi: logpartner = new _789hiLogin(); break;
                    //case ServerType._51: logpartner = new _51Login(); break;
                    //case ServerType._29ww: logpartner = new _29wwLogin(); break;
                    //case ServerType.Qidian: logpartner = new QidianLogin(); break;
                }
            }
            catch (Exception ex)
            {
                UiUtils.getInstance().error("Error:::" + ex.StackTrace);
            }
            return logpartner;
        }
        public bool ifSupportServer(ServerType stype) 
        {
            return getLoginImpl(stype) != null;
        }
    }
}
